<template>
  <div class="prompt-container" v-if="isVisible">
    <div class="prompt-box">
      <div class="prompt-title">
        <div style="display: flex; align-items: center">
          <svg-icon
            icon-class="mima_icon"
            style="width: 16px; height: 16px; margin-right: 8px"
          />
          <div>修改密码</div>
        </div>
        <el-icon color="#fff" @click="cancelPrompt" size="16"><Close /></el-icon>
      </div>
      <div class="prompt-content">
        <div>
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            
            label-width="auto"
            class="demo-ruleForm"
          >
            <el-form-item label="登录名" prop="usename">
              <el-input style="width:320px" placeholder="请输入" v-model="ruleForm.usename" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input style="width:320px" placeholder="请输入" v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item label="原密码" prop="oldpsd">
              <el-input style="width:320px" type="number" placeholder="请输入" v-model="ruleForm.oldpsd" />
            </el-form-item>
            <el-form-item label="新密码" prop="newpsd">
              <el-input style="width:320px" type="number" placeholder="请输入" v-model="ruleForm.newpsd" />
            </el-form-item>
            <el-form-item label="确认新密码" prop="comfimnewpsd">
              <el-input style="width:320px" type="number" placeholder="请输入" v-model="ruleForm.comfimnewpsd" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="prompt-footer">
        <el-button type="primary" @click="confirmInput">确定</el-button>
        <el-button @click="cancelPrompt">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const isVisible = ref(false);
let resolvePromise = null;
let rejectPromise = null;
let promise = new Promise((res, rej) => {
  resolvePromise = res;
  rejectPromise = rej;
});

const title = ref("标题");
const inputValue = ref("");
const sexValue = ref("");
const ageValue = ref("");

const ruleForm = ref({
  name: '',
  usename:'',
  oldpsd:'',
  newpsd:'',
  comfimnewpsd:''
})

const rules =ref({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
  ],
  usename:[
    { required: true, message: '请输入登录名', trigger: 'blur' },
  ],
  oldpsd:[
    { required: true, message: '请输入原密码', trigger: 'blur' },
  ],
  newpsd:[
    { required: true, message: '请输入新密码', trigger: 'blur' },
  ],
  comfimnewpsd:[
    { required: true, message: '请确认新密码', trigger: 'blur' },
  ],
})

const showPrompt = (newTitle) => {
  title.value = newTitle || title.value;
  isVisible.value = true;
  return promise;
};

const confirmInput = () => {
  proxy.$refs["ruleFormRef"].validate(valid => {
    if (valid) {
      isVisible.value = false;
      resolvePromise(
        JSON.stringify(ruleForm.value)
      );
      if (resolvePromise) {
        resolvePromise = null;
        rejectPromise = null;
        promise = new Promise((res, rej) => {
          resolvePromise = res;
          rejectPromise = rej;
        });
        ruleForm.value = {
          name: '',
          usename:'',
          oldpsd:'',
          newpsd:'',
          comfimnewpsd:''
        }
      }
    }
  });
};

const cancelPrompt = () => {
  isVisible.value = false;
  resolvePromise(
    JSON.stringify('取消')
  );
  if (resolvePromise) {
        resolvePromise = null;
        rejectPromise = null;
        promise = new Promise((res, rej) => {
          resolvePromise = res;
          rejectPromise = rej;
        });
        ruleForm.value = {
          name: '',
          usename:'',
          oldpsd:'',
          newpsd:'',
          comfimnewpsd:''
        }
  }
};

defineExpose({
  isVisible,
  resolvePromise,
  rejectPromise,
  promise,
  inputValue,
  showPrompt,
  confirmInput,
  cancelPrompt,
});
</script>

<style lang="scss" scoped>
.prompt-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 4px 4px 4px 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100000000000;
}

.prompt-box {
  background-color: #fff;
  width: 520px;
  border-radius: 2px;
  box-shadow: 0px 12px 32px 4px rgba(0, 0, 0, 0.04),
    0px 8px 20px rgba(0, 0, 0, 0.08);
  .prompt-title {
    font-size: 18px;
    color: #303133;
    width: 100%;
    height: 40px;
    padding: 0 16px;
    box-sizing: border-box;
    background: #697786;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    color: #ffffff;
  }
  .prompt-content {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 0;
    box-sizing: border-box;
    .prompt-content-item {
      display: flex;
      align-items: center;
      padding-bottom: 16px;
      padding-right: 16px;
      box-sizing: border-box;
      font-size: 14px;
      color: #606266;
    }
  }
}

button {
  padding: 8px 15px;
  margin: 5px;
  cursor: pointer;
}
.prompt-footer {
  width: 100%;
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #E0E0E0;
}
</style>